home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Util / conv / Acvt.lha / Acvt 1.07 / sources / acvt.cpp next >
C/C++ Source or Header  |  2001-03-08  |  5KB  |  217 lines

  1. //    This program is free software; you can redistribute it and/or modify
  2. //    it under the terms of the GNU General Public License as published by
  3. //    the Free Software Foundation; either version 2 of the License, or
  4. //    any later version.
  5. //
  6. //    This program is distributed in the hope that it will be useful,
  7. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9. //    GNU General Public License for more details.
  10. //
  11. //    You should have received a copy of the GNU General Public License
  12. //    along with this program; if not, write to the Free Software
  13. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. //
  15.  
  16. #include "pub.def"
  17. #include "wintypes.h"
  18. #include "autil.h"
  19. #include "adsk.h"
  20.  
  21. BOOL Convert( char* szInFile, char* szOutFile );
  22.  
  23. DISK_TYPE g_dtypeIn = DISK_AUTO;
  24. DISK_TYPE g_dtypeOut = DISK_ATR;
  25.  
  26. BOOL g_bVerbose = FALSE;
  27. BOOL g_bBatchMode = FALSE;
  28. BOOL g_bRepairAuto = FALSE;
  29. BOOL g_bRepair = TRUE;
  30. BOOL g_bFirstErrStop = FALSE;
  31. BOOL g_bOverWrite = FALSE;
  32. BOOL g_bTestOnly = FALSE;
  33. BOOL g_bForceClassic = FALSE;
  34.  
  35. #define SHEADER PRG_NAME " v" PRG_VERSION " (c) " PRG_COPYRIGHT " " PRG_AUTHOR "\n"
  36.  
  37. #define HEADER SHEADER \
  38.    PRG_DESC "\n" \
  39.     "  Latest version can be found at " PRG_URL "\n" \
  40.     "  Published under GPL. See GPL.TXT.\n\n"
  41.  
  42. #define USAGE HEADER "Usage:  " PRG_NAME " " PRG_USAGE
  43.  
  44. #include "switches.cpp"
  45.  
  46. int main(int argc,char *argv[])
  47. {
  48.     setbuf( stdout, NULL );
  49.     setbuf( stderr, NULL );
  50.  
  51.     if ( !SWITCHES_Init( &argc, argv ) )
  52.         return 1;
  53.  
  54.     if ( argc < 2 )
  55.     {
  56.         SWFN_HELP( USAGE );
  57.         return 1;
  58.     }
  59.  
  60.     fprintf( stderr, SHEADER );
  61.  
  62.     BOOL bRet = FALSE;
  63.  
  64.     char szOutFile[ 255 ];
  65.  
  66.     if ( g_bBatchMode )
  67.     {
  68.         argc--;
  69.         argv++;
  70.  
  71.         while( argc-- )
  72.         {
  73.             char* szInFile = *(argv++);
  74.             GuessBestFnameFromPC( szOutFile, szInFile, GetDiskTypeExt( g_dtypeOut ) );
  75.             bRet = Convert( szInFile, szOutFile );
  76.             printf( "\n" );
  77.  
  78.             if ( !bRet && g_bFirstErrStop )
  79.                 break;
  80.         }
  81.  
  82.     }
  83.     else
  84.     {
  85.         char* szInFile = argv[ 1 ];
  86.  
  87.         if ( argc >= 3 )
  88.         {
  89.             strcpy( szOutFile, argv[ 2 ] );
  90.         }
  91.         else
  92.         {
  93.             GuessBestFnameFromPC( szOutFile, szInFile, GetDiskTypeExt( g_dtypeOut ) );
  94.         }
  95.  
  96.         bRet = Convert( szInFile, szOutFile );
  97.     }
  98.  
  99.     if ( bRet && !g_bTestOnly )
  100.         printf ( "Done.\n" );
  101.  
  102.     return bRet ? 0 : 1;
  103. }
  104.  
  105. BOOL Convert( char* szInFile, char* szOutFile )
  106. {
  107.     ADisk* pDiskIn = NULL;
  108.     ADisk* pDiskOut = NULL;
  109.  
  110.     DISKINIT_RETCODE ret;
  111.  
  112.     g_dtypeIn = DISK_ATR;
  113.     if ( DI_RET_CONTINUE == ( ret = InitializeDisk( &pDiskIn, DISK_ATR, szInFile, FALSE, g_bRepair, g_bRepairAuto ) ) )
  114.     {
  115.         g_dtypeIn = DISK_DI;
  116.         if ( DI_RET_CONTINUE == ( ret = InitializeDisk( &pDiskIn, DISK_DI, szInFile, FALSE, g_bRepair, g_bRepairAuto ) ) )
  117.         {
  118.             g_dtypeIn = DISK_DCM;
  119.             if ( DI_RET_CONTINUE == ( ret = InitializeDisk( &pDiskIn, DISK_DCM, szInFile, FALSE, g_bRepair, g_bRepairAuto ) ) )
  120.             {
  121.                 g_dtypeIn = DISK_SCP;
  122.                 if ( DI_RET_CONTINUE == ( ret = InitializeDisk( &pDiskIn, DISK_SCP, szInFile, FALSE, g_bRepair, g_bRepairAuto ) ) )
  123.                 {
  124.                     g_dtypeIn = DISK_XFD;
  125.                     if ( DI_RET_CONTINUE == ( ret = InitializeDisk( &pDiskIn, DISK_XFD, szInFile, FALSE, g_bRepair, g_bRepairAuto ) ) )
  126.                     {
  127.                         printf( "Unable to determine format of disk image '%s'!\n", szInFile );
  128.                         return FALSE;
  129.                     }
  130.                 }
  131.             }
  132.         }
  133.     }
  134.  
  135.     if ( ret == DI_RET_CANT_CONTINUE )
  136.         return FALSE;
  137.  
  138.     printf( "Input file '%s' (%s)\n", szInFile, GetDiskTypeName( g_dtypeIn ) );
  139.  
  140.     if ( g_bVerbose )
  141.     {
  142.         DISK_GEOMETRY* pgeo = pDiskIn->GetGeometry();
  143.         printf( "Sides: %d\n", pgeo->iSides );
  144.         printf( "Tracks: %d\n", pgeo->iTracks );
  145.         printf( "SecPerTrack: %d\n", pgeo->iSectorsPerTrack );
  146.         printf( "BytesPerSec: %d\n", pgeo->iBytesPerSector );
  147.         printf( "Sectors: %d\n", pgeo->iSectors );
  148.     }
  149.  
  150.     if ( g_bTestOnly )
  151.     {
  152.         printf( "File loaded OK.\n" );
  153.         delete pDiskIn;
  154.         return TRUE;
  155.     }
  156.  
  157.     switch( g_dtypeOut )
  158.     {
  159.         case DISK_ATR:
  160.             pDiskOut = new CAtr();
  161.             break;
  162.  
  163.         case DISK_XFD:
  164.             pDiskOut = new CXfd();
  165.             break;
  166.  
  167.         case DISK_DI:
  168.             pDiskOut = new CDi();
  169.             break;
  170.  
  171.         case DISK_SCP:
  172.             pDiskOut = new CScp();
  173.             break;
  174.  
  175.         case DISK_DCM:
  176.             pDiskOut = new CDcm();
  177.             break;
  178.  
  179.         default:
  180.             break;
  181.     }
  182.  
  183.     if ( !pDiskOut )
  184.     {
  185.         fprintf( stderr, "Can't create such image!\n" );
  186.         return FALSE;
  187.     }
  188.  
  189.     DISK_GEOMETRY* pgeo = pDiskIn->GetGeometry();
  190.     DISK_GEOMETRY forced;
  191.  
  192.     memcpy( &forced, pgeo, sizeof( DISK_GEOMETRY ) );
  193.  
  194.     if ( g_bForceClassic )
  195.         ForceClassicSize( &forced );
  196.  
  197.     //duplicating the image
  198.     pDiskOut->Duplicate( pDiskIn, &forced );
  199.  
  200.     printf( "Output file '%s' (%s)\n", szOutFile, GetDiskTypeName( g_dtypeOut ) );
  201.  
  202.     //and save it in new format
  203.     if ( !pDiskOut->Save( szOutFile, g_bOverWrite ) )
  204.     {
  205.         printf( "Error! %s\n", pDiskOut->GetLastError() );
  206.         delete pDiskIn;
  207.         delete pDiskOut;
  208.         return FALSE;
  209.     }
  210.  
  211.     delete pDiskIn;
  212.     delete pDiskOut;
  213.  
  214.     return TRUE;
  215. }
  216.  
  217.